﻿
package Memoir 
{
	/**
	 * Game state governing the client's viewpoint.
	 */
	import Main.*;
	
	import org.flixel.*;
	public class OldLadyState extends FlxState
	{
	//	[Embed(source='../Assets/MissBrooks.ttf', fontFamily="brooks",embedAsCFF="false")] private var Brooks:String;
	//	[Embed(source='../Assets/valerie.ttf', fontFamily="val",embedAsCFF="false")] private var Val:String;
		[Embed(source='../Assets/segoesc.ttf', fontFamily="segoe",embedAsCFF="false")] private var Segoe:String;
		
		private var level:int;
		private var alignment:int;		
		
		private static const font:String = "segoe";
		private static const fontSize:uint = 24;
		private static const color:uint = 0xFF000000;
		private static const align:String = "left";
		private static const top:uint = 20;
		private static const br:String = '\n\n';
		
		private var content:String;		
		private var txt:FlxText;		
		private var mem:String;
		private static var numEnds:uint = 0;
		
		private static var endings:Array = new Array();
				
		public function OldLadyState(lvlarg:int, alignarg:int=0, memarg:String="",filmstrip:FlxSprite=null):void
		{
			
			level = lvlarg;
			alignment = alignarg;
			mem = memarg;
			if (mem != ""){
				mem = mem + " ";
			}
			
			if(filmstrip){
				filmstrip.reset(filmstrip.x,10);
				defaultGroup.add(filmstrip);
			}
			
			FlxState.bgColor = 0xFFf3f6f3;
			/*Levels:
			* 0-Park
			* 1-Adolesence
			* 2-Young Adulthood
			* 3-Adulthood
			* 4-Midlife
			* 5-Final
			*/
			
			
			switch (level) {
				case 10: //fired!
					if(endings.indexOf(10)==-1){ numEnds = endings.push(10); }
					content = "Hmm... Nothing here inspires me. Maybe this collaboration was a bad idea. " +
						"Thanks for you time though. I'm sure you can find someone else who could make use " +
						"of your skills.";
					show();
					break;
				case 0: 
					//Park
					content = mem + "You're the freelance photographer who called?  Thanks for meeting with me." 
					+ br + "The project I'm writing is my memoir. I want to get a feel for your style and test your skill. " +
					br + "Go ahead and take some pictures here in the park.";
					show();	
					txt = new FlxText(0, FlxG.height-60, FlxG.width, "[Press X or click to Continue.]");
					txt.setFormat(null, 12, 0xaaaaaa,"center");
					this.add(txt);		
					break;
				case 1:
					content = mem + "You know, I didn't have a normal childhood. I grew up in an orphanage. ";
					switch(alignment) {
						case 1: // Zoo
							content += "My parents adopted me when I was seven. " +
							"I had my first real birthday party with them. I was so happy to finally have a family." + br +
							"Take some pictures that remind me of my childhood. " +
								"I want to be reminded of youth.";
							show();
							break;
						case 0: // Zoo
							content +=  "Every year, they would take all the kids to the zoo. " +
								"I always looked forward to that trip. It was the only thing that broke out of the monotony of being at the orphanage every day."
								+ br + "Take some pictures that remind me of my childhood. " +
								"I want to be reminded of youth.";
							show();
							break;
						case -1: // Bar
							content += "I stayed with a foster family for a while. " +
								"The 'mother' drank a lot, and the 'father' sometimes got violent." +
								br + "Looking back, I think they didn't get what they wanted " +
								"out of life. They took that out on us. " +
								"Get me a picture of discontent.";
							show();
							break;
					}
					break;
				case 2:                          
					content = "In high school, I did okay academically. " +
						"And I had a lot of friends, though none of those friendships ran very deep." + br;
					switch(alignment) {
						case 1: // Suburb
							content += mem + "I was close to my adoptive parents. " +
								//"Not many girls knew how to drive back then, but my father showed me how anyway. " +
								//"I'm so grateful I was adopted by such great parents." + br + 
									//"M" +
									"When I turned 16, m" + 
								"y dad taught me to drive in an corn field. It's been developed since....  " + 
								"Still, why don't you go there and take a picture for me? This is the address.";
							show();
							break;
						case 0: // Mall
							content +=  "I didn't excel academically, or explore my passions. " +
								"I didn't have passions. Those four years mean nothing to me now."
							+ br + mem + "My teachers seemed convinced that I'd be destined for menial labor. " +
								"I wonder how differently my life would have looked.";
							show();
							break;
						case -1: // Mall
							content += mem + "My boyfriend Jamie was the only good part of my life as a teenager. " +
								"But in his junior year, he committed suicide."
								+ br +  "I still don't know why; " +
								"everyone said it was just teen angst. He felt he wasn't good at anything," 
								+ "but he was a good boy. I wonder what his life would've been like.";
							show();
							break;
					}
					break;
				case 3:
					switch(alignment) {
						case 1: // Coffee House
							content = mem + "Overall I would say I had a great college experience! " +
								"I managed to get a degree in literature, despite partying."
								+ br + "I had such good times in college. " +
								"I miss being that comfortable with people.";
							show();
							break;
						case 0: // Office
							content = mem + "I worked as a secretary for a while, when I was just out of college,  " +
								"but my boss made the job horrible."
								+ br + "Part of the reason it was so hard was because I was a young woman working in the 60's." +
								" Go take a picture of a modern office.";
							show();
							break;
						case -1: // Library
							content = mem + "I discovered in my twenties that I could never have children. " +
								"After talking it over with my boyfriend, we decided to break up."
								+ br + "My parents were focused on their divorce at the time, " +
								"and with my boyfriend gone, I didn't have any emotional support. I tried to escape from it all by reading, but I wish there had been someone there to comfort me.";
							show();
							break;
					}
					break;
				case 4:
					switch(alignment) {
						case 1: // TV Studio
							content = mem +  "In the next few years, I became one of the best TV anchors in the region. " +
								"Everyone knew me.  My co-anchor Steve and I bathed in success. " +
								"Steve slowed down when his wife Abby died, but at least we were still famous."
								+ br + "My success at the TV studio was a huge part of my life; get me a picture to remind me of my job.";
							show();
							break;
						case 0: // TV Studio
							content = mem +  "A few years out of school, I a new job with a man named Steve.  Steve, his wife Abby, and I were best friends for years.  " +
								"Abby died when their daughter was born, and Steve and I grew to rely on each other's friendship."
								+ br + "Working in the TV studio with Steve was a huge part of my life; " +
								"get me a picture to remind me of my job.";
							show();
							break;
						case -1: // Cemetary
							content = mem + "For a few years, I didn't want to go home. I spent 14 hours at work each day. I befriended a coworker and his wife, Steve and Abby. The three of us made plans to spend time together but never did.  Even when Abby died in childbirth, I wasn't really there for Steve."
								+ br + "A few years later, Steve died.  " +
								"He's buried next to Abby.  Please take a picture there.";
							show();
							break;
					}
					break;
				case 5:
					content = "Steve died of cancer when he was still young. Steve and Abby's daughter Denise became an orphan when he passed, and I adopted her." + br;
					switch(alignment) {
						case 1:
							content += mem +  "She's a young woman now, starting med school this year.  " +
								"Helping her grow is the most fulfilling, beautiful, heartbreaking thing I have done.";
							show();
							break;
						case 0:
							content += mem +  "Denise was a sad child. I did what I had to do to " +
								"live up to my responsibility and take care of her, but we could never really be a family.";
							show();
							break;
						case -1:
							content += mem + "I was forced to change my job, my apartment, my life. " +
								"I gave up everything for Denise.";
							show();
							break;
					}
					break;
				case 6:
					alignment = calculateOverallAlignment();
					switch(alignment) {
						case 8:
							//pos-pos: memoir published as a celebration of life, strives to become an author (pretentious?)
							content = "Looking back at my life, I'm so satisfied with all of my accomplishments. " +
								"After I publish this memoir, I'll keep writing about my experiences -- and seeking out " +
								"new ones.";
							show();
							break;
						case 7:
							//nut-pos: didn't lead an interesting life, won't publish, has been helped by process, keeps writing
							content = "Looking back at my life, I've done great things. " +
								"So my younger years weren't that interesting. So what?" +
							//	"I enjoyed my life, but maybe it's not quite worthy of a memoir. " +	
								"My story wouldn't make a good memoir, but I think I'll keep writing and see where it gets me.";
							show();
							break;
						case 6:
							//pos-nut: didn't lead an interesting life, won't publish, has been helped by process, keeps writing
							content = "Looking back at my life, I was so happy when I was young. " +
							"That's not really enough for a memoir, but it was good to remember better times." +
							"Maybe I'll keep writing and see where it gets me.";
							show();
							break;
						case 5:
							//pos-neg: past her prime
							content = "Looking back at my life, everything went my way when I was young, " +
								"but I got burdened down with life as I got older. Should I even " +
								"try publishing this memoir? I'm past my prime -- who would read it?";
							show();
							break;
						case 4:
							//neg-pos: hopeful for the future, will publish, try to become a creative writer  
							content = "Looking back at my life, I had a rough start. But I've accomplished " +
								"great things since. Now that I've retired, " +
								"I'll publish this memoir and see how far I can go.";
							show();
							break;
						case 3:
							//nut-nut: didn't lead an interesting life, won't publish, won't write, goes to nursing home
							content = "Looking back at my life has shown me how dull its been. " +
							"It's not worthy of a memoir."; // + br + "I'm too old to start a writing career anyhow. " +
						//	"Perhaps I'll live out the rest of my days quietly in a nursing home.";
							show();
							break;						
						case 2:
							//neg-nut: didn't lead an interesting life, won't publish, won't write, goes to nursing home
							content = "Looking back at my life, I had a hard time when I was young, " +
								"but overall my life has not been that extraodinary. It's not quite worthy of a memoir. " +
								"Perhaps I'll search for other things to write about."
							show();
							break;
						case 1:
							//nut-neg: didn't lead an interesting life, won't publish, won't write, goes to nursing home
							content = "Looking back at my life, I might have had some hard times recently," +
								"but my life is not nearly interesting enough for a memoir. Perhaps now that I'm retired" +
								"I'll just live out the rest of my days quietly in a nursing home."
							show();
							break;
						case 0:
							//neg-neg: has nothing to live for; commits suicide
							content = "Looking back on my life has just reminded me how much pain and hardship I've endured over the years. " +
								"Perhaps it's time for me to end it and finally be at peace.";
							show();
							break;
					}
					break;
			}
			
		}
		
		public function show():void{
			txt = new FlxText(0,120, FlxG.width,content);	
			txt.setFormat(font, fontSize, color, align);
			this.add(txt);
		}
		
		override public function update():void {
			if (FlxG.keys.pressed("X") || FlxG.mouse.justReleased())
			{
				FlxG.fade.start(0xff000000, 1, onFade);
			}
			super.update();
		}
		
		/**
		 *  Calculates alignment over course of all levels.
		 **/
		private function calculateOverallAlignment():int {
			var start:String = calcSecAlign(0, 2, 2);
			var end:String = calcSecAlign(2, 4, 2);
			trace(Main.Memoir.alignments);
			trace("start: " + start + "end: "+ end);
			if (start == Main.Memoir.POSITIVE && end == Main.Memoir.POSITIVE) {
				if(endings.indexOf(8)==-1){ numEnds = endings.push(8); }
				return(8);
			} else 	if (start == Main.Memoir.NEUTRAL && end == Main.Memoir.POSITIVE) {
				if(endings.indexOf(7)==-1){ numEnds = endings.push(7); }
				return 7;
			} else if (start == Main.Memoir.POSITIVE && end == Main.Memoir.NEUTRAL) {
				if(endings.indexOf(6)==-1){ numEnds = endings.push(6); }
				return 6;
			} else 	if (start == Main.Memoir.POSITIVE && end == Main.Memoir.NEGATIVE) {
				if(endings.indexOf(5)==-1){ numEnds = endings.push(5); }
				return 5;
			} else 	if (start == Main.Memoir.NEGATIVE && end == Main.Memoir.POSITIVE) {
				if(endings.indexOf(4)==-1){ numEnds = endings.push(4); }
				return 4;
			} else  if (start == Main.Memoir.NEUTRAL && end == Main.Memoir.NEUTRAL) {
				if(endings.indexOf(3)==-1){ numEnds = endings.push(3); }
				return 3;
			} else if (start == Main.Memoir.NEGATIVE && end == Main.Memoir.NEUTRAL) {
				if(endings.indexOf(2)==-1){ numEnds = endings.push(2); }
				return 2;
			} else if (start == Main.Memoir.NEUTRAL && end == Main.Memoir.NEGATIVE) {
				if(endings.indexOf(1)==-1){ numEnds = endings.push(1); }
				return 1;
			} else  {//if (start == Main.Memoir.NEGATIVE && end == Main.Memoir.NEGATIVE) {
				if(endings.indexOf(0)==-1){ numEnds = endings.push(0); }
				return 0;
			}
		}
		
	
		/**
		 * Determines alignment for a section of the OldLady's life.
		 **/
		private function calcSecAlign(start:int, end:int, diff:int):String {
			var pos:int = 0;
			var neg:int = 0;
			var neu:int = 0;
			
			for (var i:int = start; i < end; i++) {
				if (Main.Memoir.alignments[i] == Main.Memoir.POSITIVE) {
					pos ++;
				} else if (Main.Memoir.alignments[i] == Main.Memoir.NEUTRAL) {
					neu ++;
				} else {
					neg ++;
				} 
			}
			
			if (pos >= diff) {
				return Main.Memoir.POSITIVE;
			} else if (neg >= diff) {
				return Main.Memoir.NEGATIVE;
			} else {
				return Main.Memoir.NEUTRAL;
			}
		}
		
		
		private function onFade():void {
			switch (level) {				
				case 0:
					FlxG.state = new PlayStatePark();
					break;
				case 1:
					switch(alignment) {
						case 1:
							FlxG.state = new PlayStateZoo();
							break;
						case 0:
							FlxG.state = new PlayStateZoo();
							break;
						case -1:
							FlxG.state = new PlayStateBar();
							break;
					}
					break;
				case 2:
					switch(alignment) {
						case 1:
							FlxG.state = new PlayStateSuburb();
							break;
						case 0:
							FlxG.state = new PlayStateMall();
							break;
						case -1:
							FlxG.state = new PlayStateMall();
							break;
					}
					break;
				case 3:
					switch(alignment) {
						case 1:
							FlxG.state = new PlayStateCoffeeHouse();
							break;
						case 0:
							FlxG.state = new PlayStateOffice();
							break;
						case -1:
							FlxG.state = new PlayStateLibrary();
							break;
					}
					break;
				case 4:
					switch(alignment) {
						case 1:
							FlxG.state = new PlayStateTVStudio();
							break;
						case 0:
							FlxG.state = new PlayStateTVStudio();
							break;
						case -1:
							FlxG.state = new PlayStateCemetary();
							break;
					}
					break;
				case 5:
					// load closing screen
					FlxG.state = new OldLadyState(6);
					break;
				case 6:
					FlxG.state = new EndState(numEnds);
					break;
				case 10:
					FlxG.state = new EndState(numEnds);
					break;
				
			}
			
		}
	}
}